package com.atguigu.servlet;

import com.atguigu.bean.Book;
import com.atguigu.bean.Page;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * @ Author:任留辉
 * @ Description:处理信息的增删改查
 * @ Date:Created in 17:00 2021/3/16 0016
 */
public class BookManagerServlet extends BaseServlet {

    private BookService bookService = new BookServiceImpl();

    /**
     * 查询所有的图书信息
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
  /*  protected void getBookList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//调用service处理业务逻辑，获取所有图书信息
        List<Book> list = bookService.getBookList();
        //将所有的图书信息的List集合在请求域request中进行分享
        request.setAttribute("list", list);
        //通过转发跳转到book_manager.jsp
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);
    }
*/
    /**
     * 删除图书信息
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        /**
         * int i = Integer.parseInt("123");
         * Integer i1 =new Integer("123");
         * unteger i2 = Integer.valueOf("123");
         */
        //获取请求参数，要删除的图书ID
        String bookId = request.getParameter("bookId");
        //调用service处理业务逻辑，实现删除图书的功能
        bookService.deleteBook(Integer.parseInt(bookId));
        //重定向到列表功能BookManagerServlet?method=getBookList
        response.sendRedirect(request.getContextPath()+"/BookManagerServlet?method=getPage");
    }

    /**
     * 添加图书信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void addBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String price = request.getParameter("price");
        String sales = request.getParameter("sales");
        String stock = request.getParameter("stock");
       Book book = new Book(null,title,author,Double.parseDouble(price),Integer.parseInt(sales),Integer.parseInt(stock));
        //调用service处理业务逻辑
        bookService.addBook(book);
        //重定向到列表功能BookManagerServlet?method=getBookList
        response.sendRedirect(request.getContextPath()+"/BookManagerServlet?method=getBookList");
    }

    /**
     * 通过Id查询图书信息回显
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void getBookById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String bookId = request.getParameter("bookId");
        //调用service处理业务逻辑，根据Id获取所有图书信息
       Book book = bookService.getBookById(Integer.parseInt(bookId));
       //将图书信息在请求域request中进行共享
        request.setAttribute("book", book);
        //转发到book_update.jsp
        request.getRequestDispatcher("/pages/manager/book_update.jsp").forward(request, response);
    }

    /**
     * 通过Id修改图书信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void updateBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      //获取请求参数
        String id = request.getParameter("id");
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String price = request.getParameter("price");
        String sales = request.getParameter("sales");
        String stock = request.getParameter("stock");
        Book book = new Book(Integer.parseInt(id),title,author,Double.parseDouble(price),Integer.parseInt(sales),Integer.parseInt(stock));
        //调用service处理业务逻辑
        bookService.updateBook(book);
        //重定向到列表功能BookManagerServlet?method=getBookList
        response.sendRedirect(request.getContextPath()+"/BookManagerServlet?method=getPage");

    }
//分页原理
   /* *//**
     * 获取当前页的页码
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     *//*
    protected void getBookPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //获取请求参数 。获取当前页的页码
        String pageNoStr = request.getParameter("pageNo");
        //将浏览器传输的页码转化为int类型
        int pageNo = 1;
        try {
            pageNo = Integer.parseInt(pageNoStr);
        } catch (NumberFormatException e) { }
        //设置每页显示的条数pageSize
        int pageSize = 4;
        //获取书的总记录数
        int count = bookService.getCount();
       //获取总页数
        int totalPage = 0;
        if(count % pageSize ==0){
            totalPage = count / pageSize;
        }else {
            totalPage = count / pageSize +1;
        }
        //处理pageNo特殊页面
        if(pageNo<=0){
            pageNo=1;
        }
        if (pageNo > totalPage){
            pageNo = totalPage;
        }
        //调用service处理业务逻辑,根据pageNo和pageSize获取分页信息
        List<Book> list = bookService.getBookPageNo(pageNo,pageSize);
        //将所有的图书信息的List集合在请求域request中进行分享
        request.setAttribute("list", list);
       //将分页相关数据在请求域
        request.setAttribute("pageNo", pageNo);
        //将总条数发送到请求域
        request.setAttribute("count", count);
        //将总页数在请求域
        request.setAttribute("totalPage", totalPage);
        //通过转发跳转到book_manager.jsp
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);

    }
*/

//page实现分页 封装


    /**
     * 获取封装了分页数据的对象page
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void getPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数，即访问的当前页的页码
        String pageNoStr = request.getParameter("pageNo");
        //创建page对象
        Page<Book> page = new Page<>();
        //为Page对象的属性赋值
        page.setPageNoStr(pageNoStr);
        page.setPageSize(4);
        //访问service处理业务逻辑，获取完整的page对象
        page = bookService.getPage(page);


        //将封装了分页相关的数据的page对象在域对象request中共享
        request.setAttribute("page", page);

        //转发到book_manager.jsp
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);
    }
}
